#include "timer.h"

namespace Timer
{
	timer::timer() : seconds(0), minutes(0), hours(0)
	{
	}

	timer::timer(const timer &t):
		seconds(t.seconds),
		minutes(t.minutes),
		hours(t.hours)
	{
	}

	timer & timer::operator=(const timer &t)
	{
		if(this != &t){
			seconds = t.seconds;
			minutes = t.minutes;
			hours = t.hours;
			return *this;
		}
		return *this;
	}
	timer::~timer() {}
	void timer::reset(){
		seconds = 0;
		minutes = 0;
		hours = 0;
	}

	void timer::add_one_second()
	{
		++seconds;
		if (seconds == 60){ // 判断是否需要进位
			// add_one_minute(); // 减少函数调用带来的开销
			++minutes;
			seconds = 0; // 重置秒数
			if (minutes == 60){
				++hours;
				minutes = 0; // 重置分钟数
			}
		}
	}

	void timer::sub_one_second(){
		if(seconds > 0){
			--seconds;
			return;
		}
		// x:x:0
		if(minutes > 0){ // x:n:0
			seconds = 59;
			--minutes;
		}else if(hours != 0){ // m:0:0
			--hours;
			minutes=59;
			seconds=59;
		}
	}

	unsigned int timer::get_seconds()
	{
		return seconds;
	}

	unsigned int timer::get_minutes()
	{
		return minutes;
	}

	unsigned int timer::get_hours()
	{
		return hours;
	}

	void timer::set_hours(unsigned int h)
	{
		this->hours = h;
	}

	void timer::set_minutes(unsigned int m)
	{
		if(m < 60){
			this->minutes = m;
		}
	}

	void timer::set_secends(unsigned int s)
	{
		if(s < 60){
			this->seconds = s;
		}
	}

	bool timer::is_zero()
	{
		int num = hours + minutes + seconds;
		if(num == 0){
			return true;
		}else{
			return false;
		}
	}
	void timer::add_one_minute()
	{
		++minutes;
		seconds = 0; // 重置秒数
		if (minutes == 60)
		{ // 判断是否需要进位
			add_one_hour();
		}
	}

	void timer::add_one_hour()
	{
		++hours;
		minutes = 0; // 重置分钟数
	}
}
